<?php
use_helper('I18N');
?>
<script type="text/javascript" language="javascript">
    $(function() {
        $("#btnSubmit").click(function(){
            $("#convertForm").submit();
        });
        $('#convertAmount').autoNumeric({
            mDec: 2
        });
        $("#convertForm").validate({
            messages : {
                transactionPassword: {
                    remote: "Security Password is not valid."
                }
            },
            rules : {
                "transactionPassword" : {
                    required : true
                    , remote: "/member/verifyTransactionPassword"
                }
            },
            submitHandler: function(form) {
                waiting();
                var convertAmount = parseFloat($('#convertAmount').autoNumericGet());
                var currentBalance = parseFloat($('#currentBalance').autoNumericGet());
                var tradingBalance = parseFloat($('#tradingBalance').autoNumericGet());
                var doAction = $("#doAction").val();

                if (doAction == "CURRENT_TO_TRADING") {
                    if (convertAmount > currentBalance) {
                        error("In-sufficient fund in current account");
                        return false;
                    }
                } else if (doAction == "TRADING_TO_CURRENT") {
                    if (convertAmount > tradingBalance) {
                        error("In-sufficient fund in trading account");
                        return false;
                    }
                }
                $('#convertAmount').val($('#convertAmount').autoNumericGet());
                form.submit();
            }
        });
    });
</script>
<div class="row">
    <div class="col-md-12">
        <h2 class="page-title"><?php echo __("Account Conversion"); ?>
            <small></small>
        </h2>
    </div>
</div>
<div class="row">
    <div class="col-md-7">
        <section class="widget">
            <header>
                <h4>
                    <i class="icon-ok-sign"></i>
                    <?php echo __("Account Conversion"); ?>
                    <small></small>
                </h4>
            </header>
            <div class="body">
                <form class="form-horizontal label-left" method="post"
                      action="/member/accountConversion"
                      data-validate="parsley"
                      id="convertForm" name="convertForm">
                    <fieldset>
                        <legend class="section">
                        <?php echo __("Account Balance")?>
                        </legend>
                        <?php include_component('component', 'alert', array('param' => $sf_user->getAttribute(Globals::SESSION_DISTID, 0))) ?>
                        <div class="row">
                            <div class="col-sm-8">
                                <div class="control-group">
                                    <label class="control-label" for="currentBalance">
                                    <?php echo __("Current Account Balance")?>
                                    </label>

                                    <div class="controls form-group">
                                        <input type="text" style="text-align: right;" name="currentBalance" id="currentBalance" disabled="disabled" placeholder="<?php echo __('Current Account Balance'); ?>" class="form-control" value="<?php echo number_format($currentAccountBalance, 2); ?>"/>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="tradingBalance">
                                    <?php echo __("Trading Account Balance")?>
                                    </label>

                                    <div class="controls form-group">
                                        <input type="text" style="text-align: right;" name="tradingBalance" id="tradingBalance" disabled="disabled" placeholder="<?php echo __('Trading Account Balance'); ?>" class="form-control" value="<?php echo number_format($tradingAccountBalance, 2); ?>"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <legend class="section">
                        <?php echo __("Account Conversion")?>
                        </legend>
                        <div class="row">
                            <div class="col-sm-8">

                                <div class="control-group">
                                    <label class="control-label" for="transactionPassword">
                                    <?php echo __("Conversion method")?>
                                    </label>

                                    <div class="controls form-group">
                                        <select id="doAction" name="doAction" class="form-control">
                                            <option value="CURRENT_TO_TRADING">Convert Current to Trading</option>
                                            <option value="TRADING_TO_CURRENT">Convert Trading to Current</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label" for="convertAmount">
                                    <?php echo __("Conversion Amount")?>
                                    </label>

                                    <div class="controls form-group">
                                        <input name="convertAmount" type="text" id="convertAmount" class="form-control"/>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label" for="transactionPassword">
                                    <?php echo __("Security Password")?>
                                    </label>

                                    <div class="controls form-group">
                                        <input name="transactionPassword" type="password" id="transactionPassword" class="form-control"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12">
                                <legend class="section">
                                    <?php echo __('NOTE :'); ?>
                                </legend>
<!--                                <ol style="background-color: #F9F2F4; color: #C7254E; list-style: decimal; padding-left: 20px;" class="help-block">-->
                                <ol class="help-block" style="list-style: decimal; padding-left: 20px;">
                                    <li><?php echo __('Current Account is ONLY for package purchase, reinvest package and is NON-WITHDRAWAL'); ?></li>
                                </ol>
                            </div>
                        </div>
                    </fieldset>
                    <div class="form-actions">
                        <button type="button" id="btnSubmit" class="btn btn-danger"><?php echo __("Submit");?></button>
                        <a href="/member/summary" class="btn btn-default"><?php echo __("Cancel");?></a>
                    </div>
                </form>
            </div>
        </section>
    </div>
</div>

<script type="text/javascript" language="javascript">
var datagrid = null;
$(function() {
    datagrid = $("#datagrid").r9jasonDataTable({
        // online1DataTable extra params
        "idTr" : true, // assign <tr id='xxx'> from 1st columns array(aoColumns);
        "extraParam" : function(aoData) { // pass extra params to server
            //aoData.push({ "name": "filterAction", "value": "CONVERT" });
        },
        "reassignEvent" : function() { // extra function for reassignEvent when JSON is back from server
            reassignDatagridEventAttr();
        },

        // datatables params
        "bLengthChange": true,
        "bFilter": false,
        "bProcessing": true,
        "bServerSide": true,
        "bAutoWidth": false,
        "sAjaxSource": "/finance/convertLogList",
        "sPaginationType": "full_numbers",
        "aaSorting": [
            [0,'desc']
        ],
        "aoColumns": [
            { "sName" : "created_on",  "bSortable": true},
            { "sName" : "transaction_type",  "bSortable": true},
            { "sName" : "credit", "bVisible" : true,  "bSortable": true},
            { "sName" : "debit",  "bSortable": true},
            { "sName" : "balance",  "bSortable": true},
            { "sName" : "remark",  "bSortable": true}
        ]
    });
}); // end function

function reassignDatagridEventAttr() {
    $("a[id=editLink]").click(function(event) {

    });
}
</script>

<div class="row">
<!--    <div class="col-md-10 col-md-offset-1">-->
    <div class="col-md-12">
        <section class="widget">
            <header>
                <h4>
                    <i class="icon-file-alt"></i>
                    <?php echo __('Account Conversion History') ?>
                </h4>
            </header>
            <div class="body">
                <div>
                    <table class="table table-striped" id="datagrid" border="0" width="100%">
                        <thead>
                        <tr>
                            <th><?php echo __('Date') ?></th>
                            <th><?php echo __('Transaction Type') ?></th>
                            <th><?php echo __('In') ?></th>
                            <th><?php echo __('Out') ?></th>
                            <th><?php echo __('Balance') ?></th>
                            <th><?php echo __('Remarks') ?></th>
                        </tr>
                        </thead>
                    </table>
                    <br>
                    <br>
                </div>
            </div>
        </section>
    </div>
</div>